package com.ruoyi.project.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mall.domain.MallNewDO;
import com.ruoyi.project.mall.domain.MallNewsClassDO;
import com.ruoyi.project.mall.service.MallNewService;
import com.ruoyi.project.mall.service.MallNewsClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商城活动咨询
 *
 * @author long
 * @email 499626561@qq.com
 * @date 2020-12-25 10:18:47
 */

@Controller
@RequestMapping("/mall/mallNew")
public class MallNewController extends BaseController {
	@Autowired
	private MallNewService mallNewService;
	@Autowired
	private MallNewsClassService mallNewsClassService;

	/*@GetMapping()
	@RequiresPermissions("mall:mallNew:mallNew")
	String MallNew(){
	    return "mall/mallNew/mallNew";
	}*/

	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNew:list')")
	@GetMapping("/list")
	public TableDataInfo  list(MallNewDO mallNew){
		{
			startPage();
			Map<String, Object> stringObjectMap = BeanUtil.beanToMap(mallNew);
			List<MallNewDO> mallNewList = mallNewService.list(stringObjectMap);
			if(mallNewList.size()>0){
				for (MallNewDO mallNewDO : mallNewList) {
					MallNewsClassDO mallNewsClassDO = mallNewsClassService.get(mallNewDO.getClassId());
					if(mallNewsClassDO!=null){
						mallNewDO.setClassId(mallNewsClassDO.getName());
					}else{
						mallNewDO.setClassId("");
					}
				}
			}
			return getDataTable(mallNewList);
		}
	}
	/**
	 * 导出商城活动咨询列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNew:export')")
	@Log(title = "商城活动咨询", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MallNewDO mallNew)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(mallNew);
		List<MallNewDO> list = mallNewService.list(stringObjectMap);
		ExcelUtil<MallNewDO> util = new ExcelUtil<MallNewDO>(MallNewDO.class);
		return util.exportExcel(list, "mallNew");
	}

	/**
	 * 获取商城活动咨询详细信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNew:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		//获取分类
		AjaxResult ajax = AjaxResult.success();
		Map<String,Object> map = new HashMap<>();
		map.put("sort","ca_at");
		map.put("order","desc");
		map.put("del_flag","0");
		ajax.put("classList",mallNewsClassService.likeList(map));
		ajax.put("data",mallNewService.get(id));
		return ajax;
	}

	/**
	 * 新增商城活动咨询
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNew:add')")
	@Log(title = "商城活动咨询", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MallNewDO mallNew)
	{

		mallNew.setDelFlag(0);
		mallNew.setCaAt(new Date());
		mallNew.setId(IdUtil.simpleUUID());
		return toAjax(mallNewService.save(mallNew));
	}

	/**
	 * 修改商城活动咨询
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNew:edit')")
	@Log(title = "商城活动咨询", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MallNewDO mallNew)
	{
		mallNew.setUpAt(new Date());
		return toAjax(mallNewService.update(mallNew));
	}

	/**
	 * 删除商城活动咨询
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNew:remove')")
	@Log(title = "商城活动咨询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		return toAjax(mallNewService.batchRemove(ids));
	}




	/*@GetMapping("/add")
	@RequiresPermissions("mall:mallNew:add")
	String add(){
	    return "mall/mallNew/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("mall:mallNew:edit")
	String edit(@PathVariable("id") String id,Model model){
		MallNewDO mallNew = mallNewService.get(id);
		model.addAttribute("mallNew", mallNew);
		List<Map<String,Object>> list = mallNewsClassService.queryAllForIdAndName(new HashMap<String,Object>());
		model.addAttribute("classList", list);
	    return "mall/mallNew/edit";
	}

	*//**
	 * 保存
	 *//*
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("mall:mallNew:add")
	public R save(MallNewDO mallNew){
	    mallNew.setId(UuidUtil.get32UUID());
	    mallNew.setDelFlag(0);
	    mallNew.setCaAt(new Date());
		if(mallNewService.save(mallNew)>0){
			return R.ok();
		}
		return R.error();
	}
	*//**
	 * 修改
	 *//*
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("mall:mallNew:edit")
	public R update( MallNewDO mallNew){
		mallNew.setUpAt(new Date());
		mallNewService.update(mallNew);
		return R.ok();
	}

	*//**
	 * 删除
	 *//*
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("mall:mallNew:remove")
	public R remove( String id){
		MallNewDO mallNew = mallNewService.get(id);
		mallNew.setDelFlag(1);
		mallNew.setUpAt(new Date());
		if(mallNewService.update(mallNew)>0){
		return R.ok();
		}
		return R.error();
	}

	*//**
	 * 删除
	 *//*
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("mall:mallNew:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MallNewDO mallNew = mallNewService.get(string);
				mallNew.setDelFlag(1);
				mallNew.setUpAt(new Date());
				mallNewService.update(mallNew);
			}
		}
	
		return R.ok();
	}*/
	
}
